package gohttp

import (
	"net/http"
	"sort"
)

type muxEntry struct {
	h       http.Handler
	pattern string
}

func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
	n := len(es)
	i := sort.Search(n, func(i int) bool {
		return len(es[i].pattern) < len(e.pattern)
	})
	if i == n {
		return append(es, e)
	}
	// we now know that i points at where we want to insert
	es = append(es, muxEntry{}) // try to grow the slice in place, any entry works.
	copy(es[i+1:], es[i:])      // Move shorter entries down
	es[i] = e
	return es
}

func deleteSorted(es []muxEntry, pattern string) []muxEntry {
	i := -1
	for j, v := range es {
		if v.pattern == pattern {
			i = j
			break
		}
	}

	if i >= 0 {
		return append(es[:i], es[i+1:]...)
	} else {
		return es
	}

}
